package com.example.demo.controller;

import com.example.demo.entity.PopInfo;
import com.example.demo.entity.User;
import com.example.demo.entity.msg;
import com.example.demo.service.ExcelService;
import com.example.demo.service.InsertService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private InsertService insertService;

    /**
     * 添加信息
     *
     * @param request
     * @param httpSession
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/put", method = RequestMethod.GET)
    public ResponseEntity<msg> put(HttpServletRequest request, HttpSession httpSession) {

        try {
            PopInfo popInfo = new PopInfo();
            msg msg = new msg();
            msg.setStatus(0);
            msg.setMsg("失败");

            String classs = request.getParameter("classs");
            String name = request.getParameter("name");
            String sex = request.getParameter("sex");
            String age = request.getParameter("age");
            String phone = request.getParameter("phone");
            System.out.println(sex);
            //判断 姓名 是否 为空
            if (name.isEmpty()) {
                msg.setStatus(0);
                msg.setMsg("邮箱为空");
                return ResponseEntity.ok(msg);
            }

            //判断 性别 是否 为空
            if (sex.isEmpty()) {
                msg.setStatus(0);
                msg.setMsg("密码为空");
                return ResponseEntity.ok(msg);
            }

            //判断 年龄 是否 为空
            if (sex.isEmpty()) {
                msg.setStatus(0);
                msg.setMsg("密码为空");
                return ResponseEntity.ok(msg);
            }

            //判断 班级 是否 为空
            if (classs.isEmpty()) {
                msg.setStatus(0);
                msg.setMsg("班级为空");
                return ResponseEntity.ok(msg);
            }

            //判断 电话 是否 为空
            if (phone.isEmpty()) {
                msg.setStatus(0);
                msg.setMsg("电话为空");
                return ResponseEntity.ok(msg);
            }

            if (!name.isEmpty() && !sex.isEmpty() && !age.isEmpty() && !classs.isEmpty() && !phone.isEmpty()) {

                Integer IntAge = Integer.parseInt(age);
                popInfo.setClasss(classs);
                popInfo.setName(name);
                popInfo.setSex(sex);
                popInfo.setAge(IntAge);
                popInfo.setPhone(phone);

                // 执行插入操作 返回 插入几条数据
                Integer insertInfo = insertService.insertInfo(popInfo);
                if (insertInfo >= 1) {
                    msg.setStatus(1);
                    msg.setMsg("成功");
                    System.out.println("成功");
                }

                return ResponseEntity.ok(msg);
            }
            return ResponseEntity.ok(msg);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @Autowired
    private UserService userService;

    /**
     * 注册
     *
     * @param name
     * @param account
     * @param possword
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "register", method = RequestMethod.GET)
    public ResponseEntity<msg> register(String name, String account, String possword) {
        try {
            User user = new User();

            msg msg = new msg();
            if (name.isEmpty()) {
                msg.setStatus(0);
                msg.setMsg("姓名为空");
                return ResponseEntity.ok(msg);
            }

            if (account.isEmpty()) {
                msg.setStatus(0);
                msg.setMsg("账号为空");
                return ResponseEntity.ok(msg);
            }

            if (possword.isEmpty()) {
                msg.setStatus(0);
                msg.setMsg("密码为空");
                return ResponseEntity.ok(msg);
            }

            if (!name.isEmpty() && !account.isEmpty() && !possword.isEmpty()) {
                user.setName(name);
                user.setAccount(account);
                user.setPossword(possword);
                Integer SelInt = userService.SelName(user);
                Integer checkAcInt = userService.checkAccount(user);
                if (SelInt >= 1 || checkAcInt >= 1) {
                    msg.setStatus(0);
                    msg.setMsg("账号已存在");
                    return ResponseEntity.ok(msg);
                }


                Integer UserInt = userService.InUser(user);

                if (UserInt >= 1) {
                    msg.setStatus(1);
                    msg.setMsg("注册成功");
                    return ResponseEntity.ok(msg);
                }
            }
            return ResponseEntity.ok(msg);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    /**
     * 登录
     *
     * @param account
     * @param pwd
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ResponseEntity<msg> login(String account, String pwd) {
        try {
            msg msg = new msg();
            msg.setStatus(0);
            msg.setMsg("登录失败");
            System.out.println(account);
            System.out.println(pwd);
            if (account.isEmpty()) {
                msg.setStatus(0);
                msg.setMsg("账号为空");
                return ResponseEntity.ok(msg);
            }

            if (pwd.isEmpty()) {
                msg.setStatus(0);
                msg.setMsg("密码为空");
                return ResponseEntity.ok(msg);
            }

            if (!account.isEmpty() && !pwd.isEmpty()) {
                User user = new User();
                user.setName(account);
                user.setPossword(pwd);
                Integer aa = userService.checkUser(account, pwd);
                List<User> bb = userService.rname(account);
                System.out.println(aa);
                System.out.println(bb.get(0).getName());

                if (aa == null || aa < 0) {
                    msg.setStatus(0);
                    msg.setMsg("登录失败");
                    return ResponseEntity.ok(msg);
                }

                if (aa >= 1) {
                    msg.setStatus(1);
                    msg.setMsg("登录成功");
                    msg.setName(bb.get(0).getName());
                    return ResponseEntity.ok(msg);
                }


            }
            return ResponseEntity.ok(msg);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @Autowired
    private ExcelService excelService;

    @ResponseBody
    @RequestMapping(value = "/excel")
    public void exportExcel(HttpServletResponse response) {
        try {
            excelService.exprotExcel(response);
            System.out.println("导出成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
